package com.huaxin.hxcloud.activity;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.huaxin.common.BaseActivity;
import com.huaxin.common.media.ImageGalleryActivity;
import com.huaxin.hxcloud.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by songxf on 2017/9/7.
 * 显示巡检单上传图片dapter
 */

public class HxTaskDetailImageAdapter extends RecyclerView.Adapter<HxTaskDetailImageAdapter.ImgHolder> {
    private List<String>   datas;
    private Context        mContext;
    private LayoutInflater mInflater;
    private BaseActivity   mActivity;

    public void setDatas(List<String> datas, Context context) {
        this.datas = datas;
        this.mContext = context;
        this.mActivity = (BaseActivity) context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public ImgHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ImgHolder(mInflater.inflate(R.layout.item_h_image, parent, false));
    }

    @Override
    public void onBindViewHolder(ImgHolder holder, int position) {
        holder.update(position);
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    class ImgHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @BindView(R.id.iv_img)
        ImageView ivImg;
        View itemView;
        int  position;

        public ImgHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.itemView = itemView;
        }

        public void update(int position) {
            this.position = position;
            Context context = mContext;
            mActivity.loadNetImg(ivImg, datas.get(position));
            itemView.setOnClickListener(this);
        }


        @Override
        public void onClick(View v) {
            ImageGalleryActivity.show(mActivity, getUrls(),
                    position, false);

        }

        private String[] getUrls() {
            return datas.toArray(new String[datas.size()]);
        }
    }
}
